package com.jing.weibo.controller.timeline;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import weibo4j.model.WeiboException;

import com.jing.system.comm.BaseController;
import com.jing.system.comm.contants.StringCons;
import com.jing.system.comm.contants.SystemCons;
import com.jing.system.comm.contants.WeiboCons;
import com.jing.system.comm.enums.RuleType;
import com.jing.system.comm.enums.UpdateStatusWeiboType;
import com.jing.system.utils.PointCurrencyUtil;
import com.jing.system.utils.freeze.FreezeShareWeiboUserUtil;
import com.jing.weibo.pojo.fans.Fansattention;
import com.jing.weibo.pojo.user.UserInfo;
import com.jing.weibo.service.user.UserInfoService;
import com.jing.weibo.ws.sina.TimelineService;

/**
 * 发布微博的Controller
 * @author jing.yue
 * @version 2012/09/11 1.0.0
 */
@Controller
public class IssueWeiboController extends BaseController {

	private static final Logger logger = Logger.getLogger(IssueWeiboController.class);

	@Autowired
	private TimelineService timelineService;
	@Autowired
	private UserInfoService userInfoService;

	/**
	 * 用户分享妞妞助手到微博
	 * @return
	 */
	@RequestMapping(value = "/timeline/json/shareWeibo")
	@ResponseBody
	public ModelMap shareWeibo(HttpServletRequest request, Fansattention fansattention) {
		UserInfo userInfo = getSessionUser(request);
		logger.info(userInfo.getNickname() + "分享妞妞助手到微博");
		String result = null;
		String shareWeiboMsg = null;
		StringBuffer errorWeiboMsg = new StringBuffer();
		Integer freezeShareWeiboNum = FreezeShareWeiboUserUtil.getShareWeiboFreezeUser(userInfo.getUsername());
		if(freezeShareWeiboNum.intValue() <= 0) {
			shareWeiboMsg = String.format(StringCons.SHAREWEIBO_FULL_MSG, FreezeShareWeiboUserUtil.SHARE_WEIBO_NUM);
			result = "share_weibo_full";
		} else {
			try {
				StringBuffer imgUrl = new StringBuffer(SystemCons.ABSOLUTE_PATH);
				imgUrl.append(File.separator).append(SystemCons.SYSTEM_SHARE_WEIBO_IMG_PATH);
				timelineService.updateStatus(WeiboCons.user_share_weibo_content, imgUrl.toString(), userInfo.getToken());

				//点击“分享它”
				PointCurrencyUtil pointCurrencyUtil = new PointCurrencyUtil(RuleType.TYPE_SHARE_WEIBO, userInfo.getUsername());
				pointCurrencyUtil.handlePointCurrency();
				//同步Session中的用户信息
				setSessionUser(request, userInfoService.findByUserName(userInfo.getUsername()));

				//添加分享它的冻结次数
				FreezeShareWeiboUserUtil.addShareWeiboFreezeUser(userInfo.getUsername(), FreezeShareWeiboUserUtil.UPDATE);

				shareWeiboMsg = String.format(StringCons.SHAREWEIBO_MSG, freezeShareWeiboNum);
				result = SUCCESS;
			} catch (WeiboException e) {
				logger.error(WeiboCons.getErrorMsg(e));
				result = ERROR_WEIBO;
				errorWeiboMsg.append("调用微博接口出现异常[").append(WeiboCons.getErrorMsg(e)).append("]!");
			} catch (Exception e) {
				result = ERROR;
				logger.error(userInfo.getNickname() + ": 用户分享妞妞助手到微博异常!", e);
			}
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("result", result);
		modelMap.put("shareWeiboMsg", shareWeiboMsg);
		modelMap.put("errorWeiboMsg", errorWeiboMsg.toString());
		return modelMap;
	}

	/**
	 * 公用发送微博信息[有处理妞币和积分信息]
	 * @return
	 */
	@RequestMapping(value = "/timeline/json/userUpdateStatusWeibo")
	@ResponseBody
	public ModelMap userUpdateStatusWeibo(HttpServletRequest request, String type) {
		UserInfo userInfo = getSessionUser(request);
		logger.info(userInfo.getNickname() + "发送微博信息");
		String result = null;
		StringBuffer errorWeiboMsg = new StringBuffer();
		try {
			UpdateStatusWeiboType updateStatusWeiboType = UpdateStatusWeiboType.getCodeByUpdateStatusWeiboType(type);
			timelineService.updateStatus(updateStatusWeiboType.getText(), userInfo.getToken());

			//点击“发微博”
			PointCurrencyUtil pointCurrencyUtil = new PointCurrencyUtil(updateStatusWeiboType.getRuleType(), userInfo.getUsername());
			pointCurrencyUtil.handlePointCurrency();
			//同步Session中的用户信息
			setSessionUser(request, userInfoService.findByUserName(userInfo.getUsername()));
			result = SUCCESS;
		} catch (WeiboException e) {
			logger.error(WeiboCons.getErrorMsg(e));
			result = ERROR_WEIBO;
			errorWeiboMsg.append("调用微博接口出现异常[").append(WeiboCons.getErrorMsg(e)).append("]!");
		} catch (Exception e) {
			result = ERROR;
			logger.error(userInfo.getNickname() + ": 用户发送微博信息异常!", e);
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("result", result);
		modelMap.put("errorWeiboMsg", errorWeiboMsg.toString());
		return modelMap;
	}

	/**
	 * 发送微博信息
	 * @return
	 */
	@RequestMapping(value = "/timeline/json/sendUpdateStatusWeibo")
	@ResponseBody
	public ModelMap sendUpdateStatusWeibo(HttpServletRequest request, String content) {
		UserInfo userInfo = getSessionUser(request);
		logger.info(userInfo.getNickname() + "发送微博信息");
		String result = null;
		StringBuffer errorWeiboMsg = new StringBuffer();
		try {
			timelineService.updateStatus(content, userInfo.getToken());
			result = SUCCESS;
		} catch (WeiboException e) {
			logger.error(WeiboCons.getErrorMsg(e));
			result = ERROR_WEIBO;
			errorWeiboMsg.append("调用微博接口出现异常[").append(WeiboCons.getErrorMsg(e)).append("]!");
		} catch (Exception e) {
			result = ERROR;
			logger.error(userInfo.getNickname() + ": 用户发送微博信息异常!", e);
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("result", result);
		modelMap.put("errorWeiboMsg", errorWeiboMsg.toString());
		return modelMap;
	}
}